package pl.grabar.clinic.client.web.actions.doctor.visit;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import pl.grabar.clinic.core.dao.Dao;
import pl.grabar.clinic.core.domain.person.Doctor;
import pl.grabar.clinic.core.domain.person.Patient;
import pl.grabar.clinic.core.domain.person.data.PatientRecord;
import pl.grabar.clinic.core.domain.visit.Visit;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@Action("save")
@InterceptorRef("grabarStack")
@Results({ @Result(name = "success", type = "redirectAction", location = "list"), @Result(name = "input", location = "visit.jsp") })
public class SaveVisitAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = -3706488350222963730L;

	private static final Log LOG = LogFactory.getLog(SaveVisitAction.class);

	@Autowired
	private Dao<Visit> visitDao;

	@Autowired
	private Dao<Patient> patientDao;

	private String visitId;

	private Visit visit;

	private Visit existingVisit;

	private Patient existingPatient;

	@Override
	public void prepare() throws Exception {
		if (StringUtils.isNotBlank(visitId)) {
			existingVisit = visitDao.findById(visitId);
		}
		if (existingVisit.getPatient().getId() != 0) {
			existingPatient = patientDao.findById(String.valueOf(existingVisit.getPatient().getId()));
		}

		LOG.error(new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(visit));
//		LOG.error(new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(existingVisit));
	}

	@Override
	public void validate() {
		if (!hasErrors()) {
			if (existingVisit == null) {
				addActionError(getText("pl.grabar.clinic.client.web.doctor.visit.actions.no-such-visit"));
				LOG.error("Blad walidacji w trakcie zapisu wizyty. Wizyta o podanym id nie istnieje: " + visitId);
			}
			if (existingPatient == null) {
				addActionError(getText("pl.grabar.clinic.client.web.reception.patient.actions.no-such-patient"));
				LOG.error("Blad walidacji w trakcie zapisu wizyty. Pacjent o podanym id nie istnieje: " + existingVisit.getPatient().getId());
			}

		}
	}

	@Override
	public String execute() throws Exception {
		if (existingPatient.getPatientRecord() == null) {
			existingPatient.setPatientRecord(new PatientRecord());
		}
		visit.setDoctor(existingVisit.getDoctor());
//		visit.setPatient(null);
		visit.setPatient(existingVisit.getPatient());
		visit.setIsActive(false);
		
		existingPatient.getPatientRecord().getVisits().add(visit);
//		LOG.error(new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(existingPatient));
		patientDao.update(existingPatient);

		visitDao.delete(existingVisit);
		addActionMessage(getText("pl.grabar.clinic.client.web.doctor.visit.actions.save.done", new String[] { existingPatient.getFullName() }));
		LOG.info("Usunieto wizyte: " + existingVisit.getId() + " .Zaktualizowano pacjenta o id: " + existingPatient.getId() + ":"
				+ existingPatient.getFullName());
		return SUCCESS;
	}

	/* AKCESORY */

	public String getVisitId() {
		return visitId;
	}

	public void setVisitId(String visitId) {
		this.visitId = visitId;
	}

	public Visit getVisit() {
		return visit;
	}

	public void setVisit(Visit visit) {
		this.visit = visit;
	}

	public void setVisitDao(Dao<Visit> visitDao) {
		this.visitDao = visitDao;
	}

	public void setPatientDao(Dao<Patient> patientDao) {
		this.patientDao = patientDao;
	}
}
